// 基于 SnowLang Project (https://gitee.com/jcnc-org/snow) 的衍生作品
// 原始版权归属: Copyright © 2025 Ke Xu (Luke)
// 本实现版权归属: Copyright © 2025 Sany
// 遵循 Apache License 2.0 授权
package io

import (
	"bufio"
	"os"
	"regexp"
	"strings"

	"gitee.com/xntk2381/cc/vm/utils"
)

/**
 * The FileIOUtils class handles file input and output operations, primarily for reading instructions from files.
 * This class provides various methods to handle file paths, read file contents, clean up unnecessary lines in files, etc.
 * <p>
 * The primary functionality of this class is to read instructions from files and remove comments, empty lines, and other irrelevant content.
 * </p>
 */
// type FileIOUtils struct{}
/**
 * Default constructor for creating an instance of FileIOUtils.
 * This constructor is empty as no specific initialization is required.
 */

// func NewFileIOUtils() *FileIOUtils {
// 	return &FileIOUtils{}
// }

/**
 * Retrieves the file path from the command line arguments.
 * <p>
 * This method checks the number of command line arguments and extracts the file path from the arguments.
 * </p>
 *
 * @param args Command line arguments
 * @return The file path if the command line arguments contain a valid file path; otherwise, returns null.
 */
func GetFilePathFromArgs(args []string) string {
	if len(args) != 1 {
		utils.LogError("Please provide a valid file path.")
		return ""
	}
	return args[0]
}

/**
 * Reads and processes instructions from the specified file path.
 * <p>
 * This method reads each line of the file, removes comments and empty lines, and returns a list of valid instructions.
 * </p>
 *
 * @param filePath The file path
 * @return A list of processed instructions from the file. If an error occurs while reading the file, returns an empty list.
 */
func ReadInstructionsFromFile(filePath string) []string {
	file, err := os.Open(filePath)
	if err != nil {
		utils.LogError("Error reading file: " + err.Error())
		return []string{}
	}
	defer file.Close()

	var instructions []string
	scanner := bufio.NewScanner(file)

	for scanner.Scan() {
		line := cleanLine(scanner.Text())
		if isNotEmpty(line) {
			instructions = append(instructions, line)
		}
	}

	if err := scanner.Err(); err != nil {
		utils.LogError("Error scanning file: " + err.Error())
		return []string{}
	}

	return instructions
}

/**
 * Cleans up the content of each line by removing comments and trimming whitespace.
 * <p>
 * This method removes comments (sections starting with "//") from the line and trims any unnecessary whitespace.
 * </p>
 *
 * @param line The original line
 * @return The cleaned-up line with comments removed and whitespace trimmed.
 */
func cleanLine(line string) string {
	if len(line) == 0 {
		return ""
	}
	// 移除注释(//后面的内容)并去除前后空格
	re := regexp.MustCompile(`//.*`)
	return strings.TrimSpace(re.ReplaceAllString(line, ""))
}

/**
 * Checks if the line is empty.
 * <p>
 * This method checks if the line is an empty string or contains only whitespace characters.
 * </p>
 *
 * @param line The line to check
 * @return True if the line is not empty or does not contain only spaces; otherwise, returns false.
 */
func isNotEmpty(line string) bool {
	return len(line) > 0
}
